package hu.fic.weddingstorefinder.activity;

import hu.fic.weddingstorefinder.R;
import hu.fic.weddingstorefinder.db.WeddingStoreDBAdapter;
import hu.fic.weddingstorefinder.db.table.POI;
import hu.fic.weddingstorefinder.listadapter.StoreListAdapter;
import hu.fic.weddingstorefinder.parcel.StoreParcelable;
import hu.fic.weddingstorefinder.utils.Constants;
import hu.fic.weddingstorefinder.utils.Utils;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class StoreListActivity extends Activity implements OnItemClickListener {
	private static String LOG_TAG = "StoreListActivity";
	private ArrayList<StoreParcelable> storesOrderedByDistance;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.storelist_layout);
		
		ListView storeListView = (ListView)findViewById(R.id.storelistview);
		
		
		try {
			Location myLocation = getIntent().getParcelableExtra(Constants.LAST_FIX_KEY);
			if (myLocation != null) {
				Log.i(LOG_TAG, "lon: " + myLocation.getLongitude() + ", lat: " + myLocation.getLatitude());
				POI poi = new POI(WeddingStoreDBAdapter.getInstance().getDb(), this);
				poi.loadAll();
				ArrayList<StoreParcelable> stores = new ArrayList<StoreParcelable>();
				while (poi.nextRow()) {
					StoreParcelable storeParc = new StoreParcelable(poi);
					storeParc.setDistanceFromCurrentLocation(Utils.getDistanceBetweenCoordinated(myLocation.getLongitude(), myLocation.getLatitude(), storeParc.getLongitude(), storeParc.getLatitude()));
					Log.i(Constants.LOG_TAG_APP, "distance: " + storeParc.getDistanceFromCurrentLocation() + ", formatted: " + Utils.formatDistance(storeParc.getDistanceFromCurrentLocation()));
					stores.add(storeParc);
				}
				this.storesOrderedByDistance = new ArrayList<StoreParcelable>();
				
				for (StoreParcelable store : stores) {
					//Calcule the distance from current location:
					store.setDistanceFromCurrentLocation(Utils.getDistanceBetweenCoordinated(myLocation.getLongitude(), myLocation.getLatitude(), store.getLongitude(), store.getLatitude()));
					
					if (storesOrderedByDistance.size() == 0) {
						//if this is the first poi:
						storesOrderedByDistance.add(store);
					} else {
						//order by distance:
						for (int i = 0; i<storesOrderedByDistance.size(); i++) {
							StoreParcelable orderedStore = storesOrderedByDistance.get(i);
							if (orderedStore.getDistanceFromCurrentLocation() > store.getDistanceFromCurrentLocation()) {
								//[poiList insertObject:poi atIndex:i];
								storesOrderedByDistance.add(i, store);
								break;
							}
							if (i == storesOrderedByDistance.size()-1) {
								storesOrderedByDistance.add(store);
								break;
							}
						}
					}	
				}
			} else {
				POI poi = new POI(WeddingStoreDBAdapter.getInstance().getDb(), this);
				String sql = "SELECT * FROM POI ORDER BY POIName";
				poi.load(sql);
				ArrayList<StoreParcelable> stores = new ArrayList<StoreParcelable>();
				while (poi.nextRow()) {
					StoreParcelable storeParc = new StoreParcelable(poi);
					storeParc.setDistanceFromCurrentLocation(-1);
					Log.i(Constants.LOG_TAG_APP, "distance: " + storeParc.getDistanceFromCurrentLocation() + ", formatted: " + Utils.formatDistance(storeParc.getDistanceFromCurrentLocation()));
					stores.add(storeParc);
				}
				storesOrderedByDistance = stores;
			}
			
			
			StoreListAdapter storeAdapter = new StoreListAdapter(storesOrderedByDistance);
			storeListView.setAdapter(storeAdapter);
			storeListView.setOnItemClickListener(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		StoreParcelable selectedStore = storesOrderedByDistance.get(position);
		Intent intent = new Intent(this, StoreDetailsActivity.class);
		intent.putExtra(Constants.POI_PARCEL_KEY, selectedStore);
		this.startActivity(intent);
	}
	
	
}
